﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dia2Lib;
using System.IO;
using System.Runtime.InteropServices;

namespace Kokomo.Mirror.PeFormat.Symbols
{
	public class InjectedSourceFile : SourceFileBase
	{
		private IDiaInjectedSource _diaSource;

		internal InjectedSourceFile(
			IDiaInjectedSource diaSource
			)
		{
			this._diaSource = diaSource;
			//this._stream = new SourceFileStream(diaSource);
		}

		/* DispId 3 */
		public override string FileName { get { return this._diaSource.fileName; } }
		/* DispId 1 */
		public uint Crc { get { return this._diaSource.crc; } }
		/* DispId 4 */
		public string ObjectFileName { get { return this._diaSource.objectFilename; } }
		/* DispId 6 */
		public bool UsesSourceCompression { get { return this._diaSource.sourceCompression != 0; } }
		/* DispId 5 */
		public string VirtualFileName { get { return this._diaSource.virtualFilename; } }


		[Guid("AE605CDC-8105-4A23-B710-3259F1E26112")]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		interface _IDiaInjectedSource
		{
			uint crc { get; }
			ulong length { get; }
			string fileName { get; }
			string objectFilename { get; }
			string virtualFilename { get; }
			uint sourceCompression { get; }

			void get_source(
				uint cbData,
				out uint pcbData,
				[param: MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
				byte[] pbData
				);
		}

		public override Stream GetStream()
		{
			_IDiaInjectedSource sourceStream = (_IDiaInjectedSource)this._diaSource;
			byte[] data = new byte[this._diaSource.length];
			uint fetched = 0;
			sourceStream.get_source((uint)data.Length, out fetched, data);

			MemoryStream stream = new MemoryStream(data);
			return stream;
		}
	}
}
